package com.hdax.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.common.utils.PageUtils;
import com.hdax.common.utils.Query;

import com.hdax.gulimall.product.dao.PmsSpuImagesDao;
import com.hdax.gulimall.product.entity.PmsSpuImagesEntity;
import com.hdax.gulimall.product.service.PmsSpuImagesService;


@Service("pmsSpuImagesService")
public class PmsSpuImagesServiceImpl extends ServiceImpl<PmsSpuImagesDao, PmsSpuImagesEntity> implements PmsSpuImagesService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PmsSpuImagesEntity> page = this.page(
                new Query<PmsSpuImagesEntity>().getPage(params),
                new QueryWrapper<PmsSpuImagesEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 根据商品的id添加商品图片的信息
     * @param id
     * @param images
     */
    @Override
    public void saveImages(Long id, List<String> images) {

        if(images == null || images.size() == 0){

        }else {
            List<PmsSpuImagesEntity> collect = images.stream().map(image -> {
                PmsSpuImagesEntity pmsSpuImagesEntity = new PmsSpuImagesEntity();
                pmsSpuImagesEntity.setSpuId(id);
                pmsSpuImagesEntity.setImgUrl(image);
                return pmsSpuImagesEntity;
            }).collect(Collectors.toList());

            this.saveBatch(collect);
        }

    }

}